package com.lishang.location.helper.manager

import android.app.AndroidAppHelper
import android.content.Context
import androidx.core.content.edit

object SpManager {
    private val sp by lazy {
        if (context == null) {
            AndroidAppHelper.currentApplication()
                .getSharedPreferences("wx_preferences", Context.MODE_PRIVATE)
        } else {
            context!!.getSharedPreferences("wx_preferences", Context.MODE_PRIVATE)
        }

    }

    var context: Context? = null

    fun putSP(key: String, value: Any) {
        sp.apply {
            this.edit(true) {
                when (value) {
                    is String -> {
                        putString(key, value)
                    }
                    is Int -> {
                        putInt(key, value)
                    }
                    is Double -> {
                        putString(key, "$value")
                    }
                    is Float -> {
                        putFloat(key, value)
                    }
                    is Boolean -> {
                        putBoolean(key, value)
                    }
                    is Long -> {
                        putLong(key, value)
                    }
                }

            }
        }
    }

    fun getDouble(key: String): Double {

        return sp.run {
            getString(key, "0.0")!!.toDouble()
        }
    }

    fun getFloat(key: String): Float {
        return sp.run {
            getFloat(key, 0f)
        }
    }

    fun getString(key: String): String {
        return sp.run {
            getString(key, "")!!
        }
    }

    fun getInt(key: String): Int {
        return sp.run {
            getInt(key, 0)
        }
    }

    fun getBoolean(key: String): Boolean {
        return sp.run {
            getBoolean(key, false)
        }
    }

}